import {checkValueType} from "../utils/tool/object";
/**
 * 构造 select radio 效果
 * @param {自定义字段——字段值} val 
 */
export const valForFormFN = (val)=>{
    let arr = new Array();
    if(val){
       let vals = val.split(",");
       arr = vals.map((item, index, arr) => {
            return {key:item,title:item};
      })
    }
    console.log("valForFormFN_____")
    return arr;
};

/**
 * 构造 CheckBox 效果 
 * @param {自定义字段——字段值} val 
 */
export const valForCheckBoxFN = (val) =>{
    let arr = new Array();
    if(val){
    let vals = val.split(",");
    arr = vals.map((item, index, arr) => {
            return {label:item,value:item};
    })
    }
    return arr;
};

/**
 * 返回唯一标识的code
 * @param {自定义字段 对象} item 
 */
export const getOnlyCode =(item)=>{
    if(item.dataType == 3 || item.dataType == 4){
        return item.parentCode;
    }else{
        return item.code;
    }
};

/**
 * 返回 选中的map结构
 * @param {*} treeSelectKey 
 */
export const initGroupSearchDefaultVal =(treeSelectKey) =>{
    console.log("initGroupSearchDefaultVal_____")
    if(!treeSelectKey)return null;
    const map = new Map();
    map.set(treeSelectKey, "");
    console.log("初始化DefaultVal",map)
    return map;
}

/**
 * 原始数据 转换成 下拉框options
 * @param {下拉框选项集合} options 
 * @param {初始数据源} list 
 * @param {转换对象} transfObj 
 */
export const listToSelectOptionData = (options,list,transfObj) =>{
    list && list.forEach(item=>{
        options.push({
            title:item[transfObj.label],
            key:item[transfObj.value]
        });
    });

}


/**
 * 带Id结尾的字符 转换成 以Name结尾
 * @param {*} idField 
 */
export const IdFieldToNameField =(idField)=>{
    let nameField = null;
    if(idField && checkValueType("String",idField) && idField.endsWith("Id")){
        nameField = idField.substring(0,idField.lastIndexOf("Id")) + "Name";
    }else{
        nameField = idField;
    }
    // console.log("带Id结尾的字符 转换成 以Name结尾:",idField,"->结果:",nameField)
    return nameField;
}